package com.example.forum.config;

import com.example.forum.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//注册token拦截器
//把token拦截器配置添加到注解
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Autowired//把拦截器组件注入进来
    TokenInterceptor tokenInterceptor;
    //添加拦截器,将自定义拦截器进行注册
    public void addInterceptors(InterceptorRegistry registry){
        //把拦截器添加到框架中add
        InterceptorRegistration interceptorRegistration=registry.addInterceptor(tokenInterceptor);
        //对哪些路径生效
        interceptorRegistration.addPathPatterns("/api/**")
                .excludePathPatterns("error","/api/user/create","/api/token/create","/api/post/categories","/api/post/list"
                ,"/api/post/getTagOrder","/api/post/view","/api/post/month","/api/post/week","/api/user/visitor","/api/post/replyList"

                );

    }
    //全局跨域
    @Override
    public void addCorsMappings(CorsRegistry registry){
        registry.addMapping("/**")//所有接口
                .allowCredentials(true)//是否发送cookie
                .allowedOriginPatterns("*")//支持域
                .allowedMethods(new String[]{"GET","POST","PUT","DELETE"})//支持方法
                .allowedHeaders("*")
                .exposedHeaders("*");

    }
}
